<?php

namespace App\Dao;


use App\Model\AutoReply;
use App\Model\GreetMaster;
use App\Utils\MongoDBClient;
use Hyperf\Context\ApplicationContext;

class AutoReplyDao
{
    private static $instance;

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = Applicationcontext::getcontainer()->get(self::class);
        }
        return self::$instance;
    }

    // 记录脚本推送历史
    public function getReplyContentWithId($to_uid, $type, $language)
    {
        $data = $this->getData($type, $language);

        $id = 0;
        $content = '';
        if (!empty($data) && !empty($data['data'])) {
            $mongo = MongoDBClient::getInstance();
            $lastMsgWhere['client_id'] = intval($to_uid);
            $lastMsgWhere['type'] = intval($type);
            $lastMsgOption['limit'] = 1;
            $model = $mongo->find('tb_get_uid_content_ids_num', $lastMsgWhere, $lastMsgOption);
            if (empty($model)) {
                $his_send_id = [];
            } else {
                $his_send_id = !empty($model[0]['list_content_id']) ? json_decode($model[0]['list_content_id'], true) : [];
            }
            $new_send_id = empty($his_send_id) ? $data['id'] : array_diff($data['id'], $his_send_id);
            if (empty($new_send_id)) {
                $his_send_id = array_diff($his_send_id, $data['id']);
                $new_send_id = empty($his_send_id) ? $data['id'] : array_diff($data['id'], $his_send_id);
            }
            if (!empty($new_send_id)) {
                shuffle($new_send_id);
                $id = $new_send_id[0];
                $content = $data['data'][$id];
                $his_send_id[] = $id;
                $thisMsgModify['list_content_id'] = json_encode($his_send_id);
                $thisMsgModify = array_merge($lastMsgWhere, $thisMsgModify);
                $mongo->update('tb_get_uid_content_ids_num', $lastMsgWhere, $thisMsgModify, false, true);
            }
        }

        return [$id, $content];
    }


    /**
     * 获取自动回复信息
     * @param $type 0发送 1回复 2男版 3女版
     * @param $language
     * @param $limit -存在时，随机获取$limit条
     * @return array $data
     */
    public function getData($type, $language, $limit = null)
    {
        $hour = date('G');
        if (6 < $hour && $hour <= 10) {
            $sub_type = 0;                          //06:00-10:00
        } elseif (10 < $hour && $hour <= 13) {
            $sub_type = 1;                          //10:00-13:00
        } elseif (13 < $hour && $hour <= 18) {
            $sub_type = 2;                          //13:00-18:00
        } elseif (18 < $hour && $hour <= 23) {
            $sub_type = 3;                          //18:00-23:00
        } else {
            $sub_type = 4;                          //23:00-06:00
        }
        $query = AutoReply::query()->where('type', $type)->where('status', 2)->where(function ($query) use ($sub_type) {
            $query->where('sub_type', $sub_type)->orWhere('sub_type', 5);
        })->when($limit, function ($query) use ($limit) {
            $query->orderByRaw('RAND()')->limit($limit);
        }, function ($query) {
            $query->orderBy('id', 'desc');
        });

        $rs = match ($language) {
            'zh-hk' => $query->selectRaw('id,type,ctime,sub_type,hk_content as content')->get(),
            'zh-cn' => $query->selectRaw('id,type,ctime,sub_type,content')->get(),
            default => $query->selectRaw('id,type,ctime,sub_type,en_content as content')->get(),
        };

        $rs = modelToArray($rs);
        if (!empty($limit)) {
            return $rs;
        }
        $data = [];
        $id = [];
        $cont = [];
        foreach ($rs as $v) {
            $id[] = $v['id'];
            $cont[$v['id']] = $v['content'];
        }
        $data['id'] = $id;
        $data['data'] = $cont;
        return $data;
    }


    /**
     * 一键打招呼
     * @param $type
     * @param $language
     * @return  string content
     */
    public function sendGreet($type, $language)
    {
        $data = $this->getData($type, $language);
        $content = '';
        if (!empty($data) && !empty($data['data'])) {
            shuffle($data['id']);
            $content = $data['data'][$data['id'][0]];
        }

        return $content;
    }


}